import api from '../../../api/index'
import uploadOss from '../../../utils/uploadFileOss/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    name: '',
    content: '',
    fileList: [],
    imagesUrl: []
  },
  //文件选择
  afterRead(event) {
    const { file, callback } = event.detail;
    console.log('33333', file);
    const videoDuration = file.duration;
    if (videoDuration > 50) {
      // 视频时长超过50秒，进行相应处理
      console.log('视频超过50秒');
      wx.showToast({
        title: '上传失败,视频超过50秒！',
        icon: 'none',
        duration: 2000
      })
    } else {
      // 视频时长符合要求，继续上传
      console.log('视频时长符合要求');
      // console.log(file);
      var num = []
      num.push(file.url)
      num.push(file.thumb)
      console.log(num); 
      this.setData({
        imagesUrl: []
      })
      this.uploadDIY(
        num,
        0,
        0,
        0,
        num.length,
        num
      )
    }

  },
  /* 删除图片*/
  deleteImg(e) {
    let fileList = this.data.fileList;
    fileList.splice(e.detail.index, 1);
    this.setData({ fileList: fileList });
  },
  uploadDIY(filePaths, successUp, failUp, i, length, param) {
    // 正在上传
    // debugger
    param.doing = true // this.data.prove.doing = true;
    let that = this
    // wx.showLoading({
    //     title: `上传${i}/${length}张图片中`,
    //     duration: 1000
    // })

    uploadOss(filePaths[i])
      .then((res) => {
        console.log(res);
        successUp++
        i++
        let imagesUrl = this.data.imagesUrl
        imagesUrl.push(res.url)
        this.setData({
          imagesUrl
        })
        console.log(this.data.imagesUrl);
        if (i === length) {
          console.log(i);
          console.log(length);
          // 全部上传成功
          console.log(param);
          if (successUp === length) {
            param.uploadSuccess = true
            console.log('图片上传成功进入回调')
            let img = {
              url: this.data.imagesUrl[0],
              name: '图片1',
            }
            let fileList = this.data.fileList;
            fileList.push(img);
            console.log(fileList);
            let updatedDataImg =  [{
              "file": this.data.imagesUrl[1]
            }]
            let updatedData = fileList.map(item => {
              const { url, name, ...rest } = item;
              return { file: url, ...rest };
            });

            updatedData = JSON.stringify({ videos: updatedData, images: updatedDataImg })
            this.setData({ updatedData: updatedData });
            console.log(updatedData);
            this.setData({ fileList: fileList });
            // wx.hideLoading()
            // that.savetmp(param.imagesUrl[0])
          } else {
            // 图片上传部分失败
            param.doing = false
            wx.hideLoading()
            wx.showToast({
              title: '上传图片失败,请重新上传！',
              icon: 'none',
              duration: 2000
            })
          }
        } else {
          // 递归调用uploadDIY函数
          that.uploadDIY(
            filePaths,
            successUp,
            failUp,
            i,
            length,
            param
          )
        }
      })
      .catch((e) => {
        console.log('---图片上传失败 catch--- e', e)
        param.doing = false
        failUp++
        wx.hideLoading()
        wx.showToast({
          title: '图片上传失败，请重新上传！',
          icon: 'none',
          duration: 2000
        })
      })
  },

  formSubmit(e) {
    console.log(e);
    let _that = this;
    let name = e.detail.value.name;
    let content = e.detail.value.content;
    if (name == "") {
      wx.showModal({
        title: '提示',
        content: '标题不能为空!',
      })
      return false
    }
    else {
      this.setData({
        loading: true
      })
      var n = wx.getStorageSync("userinfo");
      let status = false;
      api.post('/api/hxhealth/forum/article/save', {
        data: {
          name,
          content: content ? content : '',
          media: this.data.updatedData
        }
      }).then(res => {
        wx.showToast({
          title: '提交成功',
        })
        //提示框消失后返回上一级页面
        setTimeout(() => {
          wx.navigateBack({
            change: true,
          })
        }, 1200)
      }).catch(e => {
        console.log(e)
        wx.showToast({
          icon: 'error',
          duration: 1200,
          title: '提交失败：' + e.faultstring,
        })
      })
      return status;
    }
  },
})